%This code can be executed to generate Figure 2A and 2B. Figure 2A shows
%two selected tensile data sets (SP_3 and SP_4). SP indicates the name of
%the spider the sample was taken from and the number is the sample number.
%This two capitol letter and number naming scheme is consistent for all
%other tensile data sets. On the left y-axis of Figure 2A, there is a
%scatter plot of all measured loop strengths (LOE_all). The MATLAB code 
% titled, "PlotAllLoxoCurves" will plot all the tensile data collected from 
% testing the looped recluse silk. 
% Figure 2B is a histogram of the recluse loop strengths (avg_LS) 
% normalized by the average TS of recluse silk (avg_TS). The average is 
% indicated by a black dotted line.
% Additional formatting is done using Inkscape to exactly match the 
% published Figure 2A and 2B.
% The folder titled, "Raw Data Files," must be open to run this code!

clear;

%Import and plot raw tensile (TT) and cross-sectional (CS) data
Loxo_raw_data=importdata('2016-04-26_SK_TT_raw_data.mat');
Loxo_CS_data=importdata('2016-06-01_SK_CS_raw_data.mat');

%Loop opening events (LOE) were selected manually. The following nested
%loops determine the strength of the loops (LOE_**) and the silk (TS_**)

%GT
TS_GT=[0.638; 0.625; 0.52];
LOE_GT=[0.38 0 0; 0.465 0.13 0; 0.105 0.25 0.135];
norm_LOE_GT=zeros(length(TS_GT),size(LOE_GT,2));
norm_LOE_all=[];
LOE_all=[];
for i=1:length(TS_GT)
    for j=1:size(LOE_GT,2)
        norm_LOE_GT(i,j)=LOE_GT(i,j)/TS_GT(i);
        if norm_LOE_GT(i,j)>0
            norm_LOE_all=[norm_LOE_all norm_LOE_GT(i,j)];
            LOE_all=[LOE_all LOE_GT(i,j)];
        end
    end
end
%MA
TS_MA=[0.684; 0.637];
LOE_MA=[0.185 0.15 0; 0.605 0.647 0.48];
norm_LOE_MA=zeros(length(TS_MA),size(LOE_MA,2));
for i=1:length(TS_MA)
    for j=1:size(LOE_MA,2)
        norm_LOE_MA(i,j)=LOE_MA(i,j)/TS_MA(i);
        if norm_LOE_MA(i,j)>0
            norm_LOE_all=[norm_LOE_all norm_LOE_MA(i,j)];
            LOE_all=[LOE_all LOE_MA(i,j)];
        end
    end
end
%PO
TS_PO=[0.478; 0.458];
LOE_PO=[0.34 0.205 0.025 0.045 0.06 0.12 0.035 0.14 0.11 ; 0.112 0.04 0.2 0.19 0 0 0 0 0];
norm_LOE_PO=zeros(length(TS_PO),size(LOE_PO,2));
for i=1:length(TS_PO)
    for j=1:size(LOE_PO,2)
        norm_LOE_PO(i,j)=LOE_PO(i,j)/TS_PO(i);
        if norm_LOE_PO(i,j)>0
            norm_LOE_all=[norm_LOE_all norm_LOE_PO(i,j)];
            LOE_all=[LOE_all LOE_PO(i,j)];
        end
    end
end
%SP
TS_SP=[0.739; 0.87; 0.948; 0.983];
LOE_SP=[0.105 0 0 0; 0.285 0.345 0.02 0.015 ; 0.02 0.03 0.33 0; 0.4 0.785 0 0];
norm_LOE_SP=zeros(length(TS_SP),size(LOE_SP,2));
for i=1:length(TS_SP)
    for j=1:size(LOE_SP,2)
        norm_LOE_SP(i,j)=LOE_SP(i,j)/TS_SP(i);
        if norm_LOE_SP(i,j)>0
            norm_LOE_all=[norm_LOE_all norm_LOE_SP(i,j)];
            LOE_all=[LOE_all LOE_SP(i,j)];
        end
    end
end
%AB
TS_AB=[0.816; 0.395];
LOE_AB=[0.6 0; 0.29 0.431];
norm_LOE_AB=zeros(length(TS_AB),size(LOE_AB,2));
for i=1:length(TS_AB)
    for j=1:size(LOE_AB,2)
        norm_LOE_AB(i,j)=LOE_AB(i,j)/TS_AB(i);
        if norm_LOE_AB(i,j)>0
            norm_LOE_all=[norm_LOE_all norm_LOE_AB(i,j)];
            LOE_all=[LOE_all LOE_AB(i,j)];
        end
    end
end
%AN
TS_AN=[0.395; 0.578; 0.368];
LOE_AN=[0.018 0.025 0.23 0.18 0; 0.04 0.05 0.19 0.205 0.12; 0.27 0.202 0 0 0];
norm_LOE_AN=zeros(length(TS_AN),size(LOE_AN,2));
for i=1:length(TS_AN)
    for j=1:size(LOE_AN,2)
        norm_LOE_AN(i,j)=LOE_AN(i,j)/TS_AN(i);
        if norm_LOE_AN(i,j)>0
            norm_LOE_all=[norm_LOE_all norm_LOE_AN(i,j)];
            LOE_all=[LOE_all LOE_AN(i,j)];
        end
    end
end
%BN
TS_BN=[0.578; 0.869; 0.395];
LOE_BN=[0.295 0.03 0.315 0 0 0 0; 0.255 0.16 0.4 0.6 0.3 0.7 0.125; 0.14 0.656 0 0 0 0 0];
norm_LOE_BN=zeros(length(TS_BN),size(LOE_BN,2));
for i=1:length(TS_BN)
    for j=1:size(LOE_BN,2)
        norm_LOE_BN(i,j)=LOE_BN(i,j)/TS_BN(i);
        if norm_LOE_BN(i,j)>0
            norm_LOE_all=[norm_LOE_all norm_LOE_BN(i,j)];
            LOE_all=[LOE_all LOE_BN(i,j)];
        end
    end
end
%ED
TS_ED=[0.51; 0.725];
LOE_ED=[0.03 0.008 0.14; 0.065 0 0];
norm_LOE_ED=zeros(length(TS_ED),size(LOE_ED,2));
for i=1:length(TS_ED)
    for j=1:size(LOE_ED,2)
        norm_LOE_ED(i,j)=LOE_ED(i,j)/TS_ED(i);
        if norm_LOE_ED(i,j)>0
            norm_LOE_all=[norm_LOE_all norm_LOE_ED(i,j)];
            LOE_all=[LOE_all LOE_ED(i,j)];
        end
    end
end

avg_TS=mean([TS_ED' TS_BN' TS_AN' TS_AB' TS_SP' TS_PO' TS_MA' TS_GT']);
std_TS=std([TS_ED' TS_BN' TS_AN' TS_AB' TS_SP' TS_PO' TS_MA' TS_GT']);
norm_LS=mean(norm_LOE_all);


%Plot the two selected tensile data sets
figure;
hold on
SP_3_x=(Loxo_raw_data.TT.SP_L_3.data(:,1)).*100;
SP_3_y=(Loxo_raw_data.TT.SP_L_3.data(:,2).*Loxo_CS_data.SP.A).*1000;
SP_4_x=(Loxo_raw_data.TT.SP_L_4.data(:,1)).*100;
SP_4_y=(Loxo_raw_data.TT.SP_L_4.data(:,2).*Loxo_CS_data.SP.A).*1000;

%Plot force and strain for two data sets (SP_3 & SP_4)
plot(SP_3_x,SP_3_y);
plot(SP_4_x,SP_4_y);
xmin=-1;

%Plot all loop opening event (LOE) strengths 
xx=ones(1,length(LOE_all)).*xmin;
scatter(xx,LOE_all.*1E12.*Loxo_CS_data.SP.A,30,'filled','MarkerFaceAlpha',1/3,'MarkerFaceColor',[0 0 0]...
           ,'MarkerEdgeColor',[0 0 0],'MarkerEdgeAlpha',2/3);

%Formatting the plot
xlabel('Strain (%)','FontSize',14); 
ylabel('Load (mN)','FontSize',14);
ax = gca;
ax.FontSize=14;
ylim([-0.05 5.5E-1]);
xlim([xmin*2 65]);
hold off;

%Plot the histogram with all of the loop strengths
figure;
hold on
[hist2]=histogram((LOE_all./avg_TS).*100,'BinWidth',12.5,'Normalization','count');
avg_LS=mean(LOE_all./avg_TS);
yy=linspace(0,max(hist2.Values),1000);
xx=ones(1,1000).*mean(avg_LS.*100);
plot(xx,yy,'k','LineStyle',':')
xlabel('% TS');
ylabel('count');
